package com.zhinong.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhinong.dto.FarmPageQueryDTO;
import com.zhinong.entity.Farm;
import com.zhinong.mapper.FarmMapper;
import com.zhinong.result.PageResult;
import com.zhinong.service.FarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FarmServiceImpl implements FarmService {

    @Autowired
    private FarmMapper farmMapper;
    @Override
    public PageResult pageQuery(FarmPageQueryDTO farmPageQueryDTO) {
        if(farmPageQueryDTO.getPage()==0&&farmPageQueryDTO.getPageSize()==0){
            farmPageQueryDTO.setPage(1);
            farmPageQueryDTO.setPageSize(10);
        }
        PageHelper.startPage(farmPageQueryDTO.getPage(),farmPageQueryDTO.getPageSize());
        Page<Farm> page = farmMapper.pageQuery(farmPageQueryDTO);
        long total = page.getTotal();
        List<Farm> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public Farm getById(Long id) {
        Farm farm = new Farm();
        farm = farmMapper.getById(id);
        return farm;
    }

    @Override
    public void deleteById(Long id) {
        farmMapper.deleteById(id);
    }

    @Override
    public void update(Farm farm) {
        farmMapper.update(farm);
    }
}
